//
//  EventsViewController.m
//  TodatPlay
//
//  Created by dllo on 16/9/8.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "EventsViewController.h"
#import "EventsTableViewCell.h"
#import "EventsHeadView.h"
#import "UIImageView+WebCache.h"
//model
#import "EventsRootClass.h"
#import "EventsData.h"
#import "EventsProjlist.h"
#import "CDMNetTool.h"
#import "DetailsViewController.h"

#define kTableViewCellReuse @"reuse"
@interface EventsViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *eventTableView;
@property (nonatomic, strong) EventsRootClass *rootModel;
@end

@implementation EventsViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    // 是的状态栏颜色为黑色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    // 使状态栏颜色为白色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"活动专题";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icons_back_dark"] style:UIBarButtonItemStyleDone target:self action:@selector(handleLeft:)];
    [self createTableView];
  }
- (void)handleLeft:(UIBarButtonItem *)btn{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setSpecialid:(NSString *)specialid {
    _specialid = specialid;
    [self.eventTableView reloadData];
    [self handleData];
}

- (void)createTableView {
    [self.eventTableView registerNib:[UINib nibWithNibName:@"EventsTableViewCell" bundle:nil] forCellReuseIdentifier:kTableViewCellReuse];
    self.eventTableView.bounces = NO;

}
#pragma mark - 处理数据
- (void)handleData {
    NSString *HTTPUrlString = @"http://wanapi.damai.cn/specialactivity.json?platform=4&channel_from=m360_market&source=10345&version=203004";
    NSString *specialid = [NSString stringWithFormat:@"specialid=%@", self.specialid];
    NSString *finalURL = [HTTPUrlString stringByAppendingFormat:@"&%@", specialid];
    [CDMNetTool GET:finalURL andBody:nil andHeader:nil andResponse:CDMJSON andSuccessBlock:^(id result) {
//        NSLog(@"请求成功:%@", result);
        self.rootModel = [EventsRootClass modelWithDic:result];
        [self.eventTableView reloadData];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"请求失败:%@", error);
    }];
}

#pragma mark - DataSource and Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.rootModel.data.projlist.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    EventsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuse];
    EventsProjlist *projlist = self.rootModel.data.projlist[indexPath.row];
    
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:projlist.pic] placeholderImage:[UIImage imageNamed:@"project_bg_detail"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    }];
    
    cell.titleLabel.text = projlist.title;
    cell.timestrLabel.text = projlist.timestr;
    cell.addressLabel.text = projlist.address;
    cell.categorynameLabel.text = projlist.categoryname;
    cell.likeCountLabel.text = [NSString stringWithFormat:@"%ld", projlist.likercount];
    cell.viewCountLabel.text = [NSString stringWithFormat:@"%ld", projlist.viewcount];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 150;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    EventsHeadView *headView = [[EventsHeadView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 300)];
    headView.backImageView.image = [UIImage imageNamed:@"1"];
    [headView.backImageView sd_setImageWithURL:[NSURL URLWithString:self.rootModel.data.specialimg] placeholderImage:[UIImage imageNamed:@"project_bg_detail"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
    headView.titleLabel.text = self.rootModel.data.specialtitle;
    headView.summaryLabel.text = self.rootModel.data.specialsummary;
    return headView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 300;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    DetailsViewController *detaVC = [[DetailsViewController alloc] init];
    UINavigationController *detaNavi = [[UINavigationController alloc] initWithRootViewController:detaVC];
    detaNavi.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    detaVC.parm = self.rootModel.data.projlist[indexPath.row].ID;
    [self presentViewController:detaNavi animated:YES completion:nil];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
